import request from '@/utils/request'

export interface ContractDTO {
  contractId?: string
  contractNo: string
  contractName: string
  customerId: string
  contactId?: string
  opportunityId?: string
  contractType?: 'SALES' | 'SERVICE' | 'MAINTENANCE'
  amount: number
  signedAmount?: number
  signedDate?: string
  startDate?: string
  endDate?: string
  ownerUserId?: string
  ownerDeptId?: string
  paymentTerms?: string
  deliveryTerms?: string
  attachmentIds?: string
  status?: 'DRAFT' | 'SUBMITTED' | 'APPROVING' | 'APPROVED' | 'EXECUTING' | 'COMPLETED' | 'TERMINATED'
  procInsId?: string
  remark?: string
}

export interface ContractItemDTO {
  itemId?: string
  contractId?: string
  lineNo: number
  itemName: string
  itemDesc?: string
  quantity?: number
  unitPrice?: number
  amount: number
  deliveryDate?: string
  remark?: string
}

// 合同管理API
export function pageContract(params: {
  pageNum: number
  pageSize: number
  contractNo?: string
  contractName?: string
  customerId?: string
  contractType?: string
  ownerUserId?: string
  status?: string
  startTime?: string
  endTime?: string
}) {
  return request({ url: '/crm/contract/page', method: 'get', params })
}

export function getContract(id: string) {
  return request({ url: `/crm/contract/${id}`, method: 'get' })
}

export function getContractItems(id: string) {
  return request({ url: `/crm/contract/${id}/items`, method: 'get' })
}

export function createContract(data: ContractDTO) {
  return request({ url: '/crm/contract', method: 'post', data })
}

export function updateContract(id: string, data: ContractDTO) {
  return request({ url: `/crm/contract/${String(id)}`, method: 'put', data })
}

export function saveContractItems(id: string, items: ContractItemDTO[]) {
  return request({ url: `/crm/contract/${String(id)}/items`, method: 'post', data: items })
}

export function deleteContract(id: string) {
  return request({ url: `/crm/contract/${String(id)}`, method: 'delete' })
}

export function deleteContractBatch(ids: string[]) {
  return request({ url: '/crm/contract/batch-delete', method: 'post', data: ids })
}


